package com.hoshino.momochi.controller;

import com.hoshino.momochi.model.DTO.CollectionDTO;
import com.hoshino.momochi.model.domain.CollectionBlog;
import com.hoshino.momochi.model.domain.Subscribe;
import com.hoshino.momochi.service.SubscribeService;
import com.hoshino.momochi.util.CodeEnum;
import com.hoshino.momochi.util.JsonResponse;
import com.hoshino.momochi.util.PageCheck;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@RestController
@Slf4j
public class SubscribeController {

    @Autowired
    private SubscribeService subscribeService;

    private final String USER_ID = CodeEnum.USERID.getMessage();

    /**
     * 获取 订阅收藏夹 列表
     *
     * @param request
     * @return
     */
    @RequestMapping("api/subscribe/getUserSubscribe")
    public JsonResponse getUserSubscribe(HttpServletRequest request) {
        Long uid = (Long) request.getAttribute(USER_ID);
        List<Subscribe> sl = subscribeService.getUserSubscribeCollection(uid);
        return JsonResponse.success(sl, CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取 订阅收藏夹 列表
     *
     * @param request
     * @return
     */
    @RequestMapping("api/subscribe/getSubscribe")
    public JsonResponse getSubscribe(HttpServletRequest request) {
        Long uid = (Long) request.getAttribute(USER_ID);
        List<CollectionBlog> sl = subscribeService.getUserSubscribe(uid);
        return JsonResponse.success(sl, CodeEnum.SUCCESS.getMessage());
    }


    /**
     * 订阅收藏夹
     *
     * @param request
     * @param collectionDTO 需求字段 collection_id
     * @return
     */
    @RequestMapping("api/subscribe/subscribeCollection")
    public JsonResponse subscribeCollection(HttpServletRequest request, @RequestBody CollectionDTO collectionDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Subscribe subscribe = new Subscribe();
        subscribe.setUserId(uid);
        Long cid = collectionDTO.getCollection_id();
        // 校验输入是否正确
        if (Objects.isNull(cid)) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        // 校验收藏夹是否存在
        if (Optional.ofNullable(subscribeService.checkCollectionIsLife(cid)).isEmpty()) {
            return JsonResponse.success(CodeEnum.COLLECTIONNOTEXIST.getCode(), CodeEnum.COLLECTIONNOTEXIST.getMessage());
        }
        subscribe.setCollectionId(cid);
        // 校验是否是自己的收藏夹
        if (Optional.ofNullable(subscribeService.checkHaveCollection(subscribe)).isPresent()) {
            return JsonResponse.success(CodeEnum.NOTOWN.getCode(), CodeEnum.NOTOWN.getMessage());
        }
        // 校验是否订阅过
        if (Optional.ofNullable(subscribeService.checkHaveSubscribe(subscribe)).isPresent()) {
            return JsonResponse.success(CodeEnum.ISEXIST.getCode(), CodeEnum.ISEXIST.getMessage());
        }
        // 订阅收藏夹
        subscribe.setCreateTime(new Date());
        subscribeService.addSubscribeCollection(subscribe);
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 取消订阅
     *
     * @param request
     * @param collectionDTO 需求字段 collection_id
     * @return
     */
    @RequestMapping("api/subscribe/falseSubscribe")
    public JsonResponse falseSubscribe(HttpServletRequest request, @RequestBody CollectionDTO collectionDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Long cid = collectionDTO.getCollection_id();
        Subscribe subscribe = new Subscribe();
        // 校验输入是否正确
        if (Objects.isNull(cid)) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        subscribe.setUserId(uid);
        subscribe.setCollectionId(cid);
        if (Optional.ofNullable(subscribeService.checkHaveSubscribe(subscribe)).isEmpty()) {
            return JsonResponse.success(CodeEnum.NOTPERMISS.getCode(), CodeEnum.NOTPERMISS.getMessage());
        }
        subscribeService.delSubscribeCollection(subscribe);
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }
}
